<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>销售预测</span>
      </div>
      <div class="searchAdd">
        <el-button class="addBtn" type="primary" @click="ShowAddDialog">
          预测
        </el-button>
        <el-col :span="15" style="width: 400px">
          <el-date-picker
            v-model="Page.stringTime"
            type="date"
            placeholder="选择日期"
            format="yyyy年M月d日"
            value-format="yyyy/M/d"
            @change="searchByTime()"
            :editable="false"
          >
          </el-date-picker>
        </el-col>
      </div>
      <div class="InfoTable">
        <el-table
          class="foreTellTable"
          :data="tableData"
          v-loading="loading"
          style="width: 100%"
          max-height="400px"
          stripe
        >
          <el-table-column type="expand">
            <template slot-scope="props">
              <el-form
                label-position="left"
                inline
                class="demo-table-expand"
                style="padding-left: 100px"
              >
                <el-form-item label="预测标题：">
                  <span>{{ props.row.salPredTitle }}</span>
                </el-form-item>
                <el-form-item label="生成时间：">
                  <span>{{ props.row.salPredMonth }}</span>
                </el-form-item>
                <el-form-item label="预测部门：">
                  <span>{{ props.row.salDepartment }}</span>
                </el-form-item>
                <el-form-item label="生成时间：">
                  <span>{{ props.row.updateTime }}</span>
                </el-form-item>
                <el-form-item label="最后操作人：">
                  <span>{{ props.row.salPredOperator }}</span>
                </el-form-item>
                <el-form-item label="预测结果：" style="width: 80% !important">
                  <span>{{ props.row.salResult }}</span>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column fixed prop="salPredId" label="ID" width="100">
          </el-table-column>
          <el-table-column prop="salPredTitle" label="预测结果标题" width="150">
          </el-table-column>
          <el-table-column prop="salPredMonth" label="预测时间" width="150">
          </el-table-column>
          <el-table-column prop="salDepartment" label="预测部门" width="150">
          </el-table-column>
          <el-table-column prop="updateTime" label="生成时间" width="150">
          </el-table-column>
          <el-table-column
            prop="salPredOperator"
            label="最后操作人"
            width="150"
          >
          </el-table-column>
          <el-table-column prop="salResult" label="预测结果" >
          </el-table-column>
        </el-table>
        <el-pagination
          :page-size="Page.size"
          :pager-count="7"
          :current-page="Page.current"
          layout="prev, pager, next"
          :page-count="Page.pageCount"
          @current-change="handleCurrentChange"
          ref="pageCurrent"
        >
        </el-pagination>
      </div>

      <!-- 预测对话框 -->
      <el-dialog
        title="预测"
        :visible.sync="AddDialogFormVisible"
        width="30%"
        @close="addDialogClosed"
      >
        <el-form
          ref="addDialogRef"
          label-width="65px"
          :model="addItem"
          :rules="addRules"
          style="padding: 0 20px; height: 400px; overflow-y: auto"
        >
          <el-form-item label="预测结果标题" label-width="120px" prop="title">
            <el-input v-model="addItem.title"></el-input>
          </el-form-item>
          <el-form-item label="预测时间" label-width="120px">
            <el-input v-model="addItem.time" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item
            label="指定预测部门"
            label-width="120px"
            prop="department"
          >
            <el-select v-model="addItem.department" placeholder="请选择">
              <el-option
                v-for="item in allDept"
                :key="item.deptId"
                :label="item.deptName"
                :value="item.deptName"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            label="部门规模"
            label-width="120px"
            prop="departmentSize"
          >
            <el-input v-model.number="addItem.departmentSize"></el-input>
          </el-form-item>
          <el-form-item label="客户数量" label-width="120px" prop="customCount">
            <el-input v-model.number="addItem.customCount"></el-input>
          </el-form-item>
          <el-upload
            class="upload-demo"
            drag
            action="/salePre/uploadFile"
            accept=".xlsx"
            :file-list="fileList"
          >
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">
              将文件拖到此处，或<em>点击上传</em>
            </div>
            <div class="el-upload__tip" slot="tip">只能上传xlsx文件</div>
          </el-upload>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForetell()">添加</el-button>
          <el-button @click="AddDialogFormVisible = false">关闭</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import pubsub from "pubsub-js";

export default {
  name: "foretell",
  data() {
    return {
      // 加载状态
      loading: "",
      // 当前登录员工
      loginUserName: "",
      // 添加预测窗口
      AddDialogFormVisible: false,
      // 查看预测窗口
      ShowDialogFormVisible: false,
      // 页面
      Page: {
        stringTime: null,
        current: 1,
        size: 6,
        pageCount: 1,
      },
      // 上传文件列表
      fileList: [],
      // 所有部门
      allDept: [],
      // 添加预测
      addItem: {
        title: "",
        time: "",
        department: "",
        departmentSize: "",
        customCount: "",
        operator: "",
      },

      // 表格数据
      tableData: [],
      // 添加数据规则
      addRules: {
        title: [{ required: true, message: "请输入预测标题", trigger: "blur" }],
        department: [
          {
            required: true,
            message: "请选择预测部门",
            trigger: ["blur", "change"],
          },
        ],
        departmentSize: [
          { required: true, message: "请输入部门规模" },
          {
            type: "number",
            message: "部门规模必须为数字",
            trigger: "blur",
          },
          ,
        ],
        customCount: [
          { required: true, message: "请输入客户数量" },
          {
            type: "number",
            message: "部门规模必须为数字",
            trigger: "blur",
          },
        ],
      },
    };
  },
  mounted() {
    this.getSalePredictionData();
    pubsub.subscribe("loginUser", (_, data) => {
      this.loginUserName = data;
    });
  },
  methods: {
    // 提交预测
    submitForetell() {
      this.$refs.addDialogRef.validate(async (valid) => {
        if (!valid) return;
        this.addItem.operator = this.loginUserName;
        const { data: res } = await this.$http.post(
          "/salePre/predict",
          this.addItem
        );
        if (res.status != 200) return this.$message.error(res.msg);
        this.$message.success("添加成功");
        this.AddDialogFormVisible = false;
        this.getSalePredictionData();
      });
    },
    // 通过时间查找
    searchByTime() {
      this.getSalePredictionData();
    },
    // 添加对话框
    ShowAddDialog() {
      this.addItem.time = new Date().toLocaleDateString();
      this.getAllDeptInfo();
      this.AddDialogFormVisible = true;
    },
    // 获取表格数据
    async getSalePredictionData() {
      this.loading = true;
      if (this.Page.stringTime == null) {
        const { data: res } = await this.$http.post("/salePre/all", {
          current: this.Page.current,
          size: this.Page.size,
        });
        if (res.status != 200) {
          this.loading = false;
          return this.$message.error(res.msg);
        }
        this.tableData = res.data.content;
        this.Page.pageCount = res.data.pageCount;
        this.loading = false;
      } else {
        const { data: res } = await this.$http.post("/salePre/getByMonth", {
          currentPage: this.Page.current,
          size: this.Page.size,
          stringTime: this.Page.stringTime,
        });
        if (res.status != 200) {
          this.loading = false;
          this.Page.stringTime = null;
          return this.$message.error(res.msg);
        }
        this.tableData = res.data.content;
        this.Page.pageCount = res.data.pageCount;
        this.loading = false;
      }
    },
    // 页面改变
    handleCurrentChange(currentPage) {
      this.Page.current = currentPage;
      this.getSalePredictionData();
    },
    // 获取所有部门信息
    async getAllDeptInfo() {
      const { data: res } = await this.$http.post("/saleperform/getAllDept");
      if (res.status != 200)
        return this.$message.error("系统错误，请重新登录！");
      this.allDept = res.data;
    },
    // 重置
    resetAddForm() {
      this.$refs.addDialogRef.resetFields();
      this.fileList = [];
    },
    //监听添加对话框关闭事件
    addDialogClosed() {
      this.resetAddForm();
    },
  },
};
</script>

<style scoped>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
.clearfix {
  display: flex;
  justify-content: space-between;
  align-content: center;
  padding: 0 50px;
}
.searchAdd {
  width: 93%;
  margin: 0 auto;
  height: 40px;
  margin-bottom: 20px;
}

.InfoTable {
  width: 93%;
  margin: 0 auto;
}
.addBtn {
  float: left;
  width: 100px;
  height: 40px;
  margin-right: 20px;
}
</style>
